package com.hmy.equipment.rabbitMq;

import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MessageSenderService {

    private final RabbitTemplate rabbitTemplate;
    private final FanoutExchange fanoutExchange;

    @Autowired
    public MessageSenderService(RabbitTemplate rabbitTemplate, FanoutExchange fanoutExchange) {
        this.rabbitTemplate = rabbitTemplate;
        this.fanoutExchange = fanoutExchange;
    }



    public void sendMessage(String message) {
        // fanoutExchange  fanout 类型的交换机，它会将收到的消息广播到所有绑定的队列中，不需要指定路由键
        rabbitTemplate.convertAndSend(fanoutExchange.getName(), "", message);
        System.out.println(message);
    }
}
